/**
 * 
 */
package com.door.erp.sevice.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.door.erp.dao.ProductDao;
import com.door.erp.dao.base.GenericDao;
import com.door.erp.enums.BaseInfoStatus;
import com.door.erp.model.Product;
import com.door.erp.pager.Pager;
import com.door.erp.sevice.ProductService;
import com.door.erp.sevice.base.GenericServiceImpl;

/**
 * @author xiaolongwei@gmail.com
 * 
 * 
 */
@Service(value = "productService")
public class ProductServiceImpl extends GenericServiceImpl<Product, Long>
		implements ProductService {
	@Resource(name = "productDao", type = ProductDao.class)
	private ProductDao productDao;

	@Override
	public GenericDao<Product, Long> getDao() {
		// TODO Auto-generated method stub
		return productDao;
	}

	@Override
	public List<Product> getValidateResultSet(Map<String, Object> param,
			Pager pager) {
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("status", BaseInfoStatus.NORMAL);
		if (param != null) {
			m.putAll(param);
		}
		return this.getAll(m, pager);
	}

	@Override
	public int getValidateResultSetCount(Map<String, Object> param) {
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("status", BaseInfoStatus.NORMAL);
		if (param != null) {
			m.putAll(param);
		}
		return this.getCount(m);
	}

	@Override
	public String saveProductImg(MultipartFile img, File dir) {
		if (dir == null || !dir.exists()) {
			dir.mkdirs();
		}
		if(img==null){
			return null;
		}
		if(!img.getContentType().contains("image")){
			return null;
		}
		
		String oldName = img.getOriginalFilename();
		String imgSuffix = oldName.substring(oldName.lastIndexOf(".") + 1);
		String newName = UUID.randomUUID().toString() +"." +imgSuffix;
		File newFile = new File(dir.getAbsolutePath(), newName);
	
		try {
			img.transferTo(newFile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		return newName;
	}

	@Override
	public List<Product> getValidateResultSet(Map<String, Object> param) {
		// TODO Auto-generated method stub
		return null;
	}

}
